<?php

namespace app\admin\controller;

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class Upload extends AdminBase
{

    protected $fileSize = 2225678;
    protected $fileExt = 'jpg,png,gif';
    protected $saveFolder = ''; // 图片保存目录

    public function checkUploadFileInfo() {
        $isupload = input('param.isupload'); // 1：不上传， 2：上传
        $is_domain = input('param.is_domain');
        if(input('param.folder','common')) {
            $this->saveFolder =  input('param.folder');
        }
        
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        $info = $file->validate(['size'=>$this->fileSize,'ext'=>$this->fileExt])->check();//检测上传文件信息，为了节省空间，所以并不会立即上传文件到服务器
        
        if($info) {
            if($isupload==1){
                return json(['code'=>1,'msg'=>'图片符合规定','data'=>'']);
            }elseif($isupload==2){
                $uploadInfo = $file->move(ROOT_PATH . 'public' . DS . 'upload'. DS . $this->saveFolder);
                if($uploadInfo) {
                    $data = DS .'upload'. DS . $this->saveFolder. DS .$uploadInfo->getSaveName();
                    if($is_domain){
                        $data = $this->domain.DS .'upload'. DS . $this->saveFolder. DS .$uploadInfo->getSaveName();
                    }
                    return json(['code'=>1,'msg'=>'图片符合规定','data'=> $data]);
                }else{
                    return json(['code'=>2,'msg'=>$uploadInfo->getError(),'data'=>'']);  
                }
            }
        }else{
           return json(['code'=>2,'msg'=>$file->getError(),'data'=>'']);
        }
    }


    public function uploadFile($folder = '')
    {
        $this->saveFolder = $folder;
        // 获取表单上传文件
        $file = request()->file('file');
        $uploadInfo = $file->move(ROOT_PATH . 'public' . DS . 'upload' . DS . $this->saveFolder);
        if ($uploadInfo) {
            return DS . 'upload' . DS . $this->saveFolder . DS . $uploadInfo->getSaveName();
        } else {
            return false;
        }
    }

    //上传安卓包
    public function uploadApk()
    {
        //例如/home/wwwroot/download/1，文件夹权限777
//        $file_path = config('apk_path');
        $file_path = env('APP.APK_PATH');
        // 获取表单上传文件
        $file = request()->file('file');
        $uploadInfo = $file->rule(function () {
            return md5(microtime(true));
        })->move($file_path, false);
        if ($uploadInfo) {
            chmod($file_path . $uploadInfo->getSaveName(), 0777);
            return true;
        } else {
            return false;
        }
    }


    //上传到七牛云
    public function uploadToQiNiu()
    {
        // 初始化签权对象
        $config = config('qiniu');
        $auth = new Auth($config['accesskey'], $config['secretkey']);
        $token = $auth->uploadToken($config['bucket']);

        // 构建 UploadManager 对象
        $uploadMrg = new UploadManager();

        // 上传文件到七牛
        $files = $_FILES;
        $values = array_values($files);
        $saveName = hash_file('sha1', $values[0]['tmp_name']) . time();
        list($ret, $err) = $uploadMrg->putFile($token, $saveName, $values[0]['tmp_name']);

        if ($err !== null) {
            $return = [
                'code' => 2,
                'msg' => lang('upload_failed')
            ];
        } else {
            $return = [
                'code' => 1,
                'msg' => lang('upload_success'),
                'data' => $config['domain'] . $ret['key'],
            ];
        }
        return json($return);

    }
}
